package homework.chatthree;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class ChatSeverThree {
    private ServerSocket serverSocket;
    private static final int IP = 8888;
    private String host;
    private Map<String ,PrintWriter> writerMap = new HashMap<>();

    public ChatSeverThree() {
        try {
            System.out.println("服务器启功中...");
            serverSocket=new ServerSocket(IP);

            System.out.println("服务器启功成功...");
        } catch (IOException e) {
            System.out.println("服务器启功失败...");
            e.printStackTrace();
            System.exit(1);
        }

    }

    class BRThread implements Runnable{
            BufferedReader br = null;
            String data;
            Socket socket = null;
            PrintWriter pw = null;

        public BRThread(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                br = new BufferedReader(
                        new InputStreamReader(
                                socket.getInputStream(), StandardCharsets.UTF_8));

                pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(),StandardCharsets.UTF_8)),true);
                String userName= br.readLine();
                writerMap.put(userName,pw);
                SeverToAllClient("欢迎:"+userName,socket.getRemoteSocketAddress().toString());

                while ((data=br.readLine())!=null){
                    if (data.startsWith("@")){

                    }else {
                        SeverToAllClient(data,"系统提示");
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                System.out.println(socket.getRemoteSocketAddress() + "离线啦...");
                Collection<PrintWriter> collection = writerMap.values();
                for (PrintWriter pws : collection){
                    pws.println(socket.getRemoteSocketAddress()+"离线啦...");
                }

            }
        }
    }

    public void SeverToAllClient(String data,String socket){
        System.out.println(socket+"说:"+data);
        Collection<PrintWriter> collection = writerMap.values();
        for (PrintWriter pws : collection){
            pws.println(socket+"说:"+data);
        }
    }

    public void SeverToOneClient(String data,Socket socket){

    }

    public void start(){
        Socket socket = null;
        try {
            while (true) {
                socket = serverSocket.accept();
                System.out.println("客户端：" + socket.getRemoteSocketAddress() + "上线啦...");

                new Thread(new BRThread(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new  ChatSeverThree().start();

    }
}
